/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.sys>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
  * Name: ipc_packet.h
  *
  * Purpose: ipc data transport protocol define for 'COM' Functional
  *
  * Developer:
  *   wen.gu , 2020-07-27
  *
  * TODO:
  *
  ***************************************************************************/

  /******************************************************************************
   **    INCLUDES
   ******************************************************************************/
#ifndef __PANDA_COM_RUNTIME_IPC_PACKET_H__
#define __PANDA_COM_RUNTIME_IPC_PACKET_H__

#include <cstdint>
#include <memory>
#include <string>
#include <functional>

#include "panda/sys/ipc_types.h"
#include "panda/sys/message.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/
#define IPC_PKT_START_CODE ((uint8_t)0xe5)
#define IPC_PKT_END_CODE ((uint8_t)0xe6)

/** transport packet(protocol) 
 *  |start code|size|data...|end code|
 *  start code: 1 bytes
 *  size:       4 bytes = data size
 *  data:       n bytes
 *  end code:   1 bytes
 */

/** the size of packet header, size(4)|sessionId(4)|type(1)|nameSize(1)| */
#define IPC_PKT_HDR_SIZE  10

/** the length of name size just 1 byte, so max size limit is 256 */
#define IPC_PKT_NAME_SIZE_MAX 256

/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
namespace panda
{
namespace sys 
{

enum class IpcDataParseState: uint8_t
{
    Start = 0,  /** check start code, then switch to 'Header' */
    Header,     /** receive and parse Header, then switch to 'Payload' */
    Payload,    /** receive payload data, then switch to 'End' */
    End,        /** check end code, if success then do callback. both success and fail then siwtch to 'Start' */
};

struct IpcPacketInfo
{    
    int32_t clientId = -1;
    uint32_t len = 0;
    uint32_t capacity = 0;
    uint32_t readPos = 0;
    IpcDataParseState state = IpcDataParseState::Start;
    uint8_t nameSize = 0;
    uint32_t headerReadPos = 0;

    uint32_t payloadSize = 0;
    uint32_t sessionId = 0;
    MessageType type = MessageType::kEvent; /**todo, refineme?? what default type set as */
    
    PayloadPtr payload = nullptr;
    uint8_t header[IPC_PKT_HDR_SIZE + IPC_PKT_NAME_SIZE_MAX]; /** not include start code and end code, headr and name */
    
};

struct IpcPacketParam
{
    uint32_t sessionId = 0; /**like packet id, a unique sequence */
    MessageType type = MessageType::kEvent; /**todo, refineme?? what default type set as */
    std::string name = "";
};

/******************************************************************************
**    CLASSES/FUNCTIONS DEFINITIONS
******************************************************************************/


class SYS_CLASS IpcDataParser
{
public:
    using PandaErrc = panda::core::PandaErrc;

    /** params:
     *  [1] IpcMessagePtr: received data as a message.
     */
    using DataReceiveHandler = std::function<void(MessagePtr)>;
public:
    IpcDataParser(DataReceiveHandler drh);
    ~IpcDataParser();

public:
    PandaErrc doParse(IpcPacketInfo& ici, const uint8_t* pdata, uint32_t size);
    IpcPacketData makePacket(const IpcPacketParam& ipp, const IpcPacketData& data);
    IpcPacketData makePacket(const IpcPacketParam& ipp, const uint8_t* data, uint32_t size);
protected:
    const uint8_t* onParseStart(IpcPacketInfo& ici, const uint8_t* pdata, const uint8_t* pend);
    const uint8_t* onParseHeader(IpcPacketInfo& ici, const uint8_t* pdata, const uint8_t* pend);
    const uint8_t* onParsePayload(IpcPacketInfo& ici, const uint8_t* pdata, const uint8_t* pend);    
    const uint8_t* onParseEnd(IpcPacketInfo& ici, const uint8_t* pdata, const uint8_t* pend);

    
private:
    DataReceiveHandler mHandler;
};

} /** namespace sys */
} /** namespace panda */


#endif /** !__PANDA_COM_RUNTIME_IPC_PACKET_H__ */